package com.hxd.otc.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.utils.ResourceUtil;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2019/1/7 0007.
 * description ${指纹密码对话框}
 */
public class FingerprintIdentifyDialog extends BaseDialog<FingerprintIdentifyDialog> {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.iv_fingerprint)
    ImageView mIvFingerprint;
    @BindView(R.id.tv_verification_result)
    TextView mTvVerificationResult;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;

    private String mTitleText;
    private String mContentText;
    private FingerprintIdentify mFingerprintIdentify;

    private static final int MAX_AVAILABLE_TIMES = 3;
    private static final long ERROR_TIMEOUT_MILLIS = 500;
    private static final long SUCCESS_DELAY_MILLIS = 300;
    private BaseFingerprint.IdentifyListener mIdentifyListener;

    public FingerprintIdentifyDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        widthScale(0.8f);
        return View.inflate(getContext(), R.layout.dialog_fingerprint_password, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
        mFingerprintIdentify = new FingerprintIdentify(getContext().getApplicationContext());
        mFingerprintIdentify.setSupportAndroidL(true);
        mFingerprintIdentify.init();
    }

    @Override
    public void setUiBeforShow() {
        mTvTitle.setText(mTitleText);
        mTvContent.setText(mContentText);

        if (!mFingerprintIdentify.isHardwareEnable()) {
            mTvVerificationResult.setText(R.string.the_current_device_is_not_supported);
        } else if (!mFingerprintIdentify.isRegisteredFingerprint()) {
            mTvVerificationResult.setText(R.string.you_have_not_set_your_fingerprint_yet);
        } else {
            mTvVerificationResult.setText(R.string.fingerprint_identification);
            mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.IdentifyListener() {
                @Override
                public void onSucceed() {
                    mTvVerificationResult.setText(R.string.verified_success);
                    mTvVerificationResult.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (mIdentifyListener != null) mIdentifyListener.onSucceed();
                        }
                    }, SUCCESS_DELAY_MILLIS);
                }

                @Override
                public void onNotMatch(int availableTimes) {
                    mTvVerificationResult.setText(String.format(ResourceUtil.getString(R.string.fingerprint_mismatch_sub_chance_remaining), availableTimes));
                    if (mIdentifyListener != null) mIdentifyListener.onNotMatch(availableTimes);
                }

                @Override
                public void onFailed(boolean isDeviceLocked) {
                    if (isDeviceLocked) {
                        mTvVerificationResult.setText(R.string.fingerprint_verification_has_been_locked_please_try_again_later_or_log_in_with_a_password);
                    } else {
                        mTvVerificationResult.setText(R.string.fingerprint_verification_failed_to_reach_the_maximum_number_please_try_again_later_or_login_with_password);
                    }
                    if (mIdentifyListener != null) mIdentifyListener.onFailed(isDeviceLocked);
                }

                @Override
                public void onStartFailedByDeviceLocked() {
                    mTvVerificationResult.setText(R.string.fingerprint_verification_has_been_locked_please_try_again_later_or_log_in_with_a_password);
                    if (mIdentifyListener != null) mIdentifyListener.onStartFailedByDeviceLocked();
                }
            });
        }

    }

    public FingerprintIdentifyDialog setTitleText(String titleText) {
        mTitleText = titleText;
        return this;
    }

    public FingerprintIdentifyDialog setContentText(String contentText) {
        mContentText = contentText;
        return this;
    }

    public FingerprintIdentifyDialog setIdentifyListener(BaseFingerprint.IdentifyListener identifyListener) {
        mIdentifyListener = identifyListener;
        return this;
    }

    @OnClick(R.id.tv_cancel)
    public void onClick() {
        dismiss();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mIdentifyListener != null) {
            mFingerprintIdentify.cancelIdentify();
        }
    }
}
